//
//package com.stmt.stmtdesktop.udp;
//
//import io.netty.buffer.ByteBufUtil;
//import io.netty.buffer.Unpooled;
//import io.netty.channel.ChannelHandlerContext;
//import io.netty.channel.ChannelId;
//import io.netty.channel.SimpleChannelInboundHandler;
//import io.netty.channel.socket.DatagramPacket;
//import io.netty.handler.timeout.IdleState;
//import io.netty.handler.timeout.IdleStateEvent;
//import io.netty.util.CharsetUtil;
//import java.net.InetSocketAddress;
//import java.nio.charset.StandardCharsets;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//
///**
// * @author liangbo
// */
//@Slf4j
//@RequiredArgsConstructor
//public class NettyUdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
//
//
//  @Override
//  public void channelActive(ChannelHandlerContext ctx) {
//
//    //获取连接通道唯一标识
//    var channelId = ctx.channel().id();
//
//    //如果map中不包含此连接，就保存连接
//    System.out.println("客户端【" + channelId + "】是连接状态");
//
//  }
//
//  @Override
//  public void channelInactive(ChannelHandlerContext ctx) {
//
//    InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
//
//    String clientIp = insocket.getAddress().getHostAddress();
//
//    ChannelId channelId = ctx.channel().id();
//
//    System.out.println("客户端【" + channelId + "】退出netty服务器[IP:" + clientIp + "--->PORT:" + insocket.getPort() + "]");
//  }
//
//
//  @Override
//  protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {
//    var content = msg.content();
//    var remoteAddress = msg.sender();
//    var localAddress = msg.recipient();
//    var body = new String(ByteBufUtil.decodeHexDump(ByteBufUtil.hexDump(content)), CharsetUtil.UTF_8);
//
//    System.out.println("udp 接收数据 remoteAddress {} localAddress " + remoteAddress + localAddress + body);
//    var request = new DefaultServerRequest(localAddress, remoteAddress, body, TcpIpType.UDP);
//    var res = handlerManager.handler(request);
//    System.out.println("udp 返回数据 " + res);
//    if (res != null && res.length() > 0) {
//      DatagramPacket sendPacket = new DatagramPacket(Unpooled.copiedBuffer(res.getBytes(StandardCharsets.UTF_8)), msg.sender());
//      ctx.writeAndFlush(sendPacket);
//    }
//  }
//
//
//  @Override
//  public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
//
//    var socketString = ctx.channel().remoteAddress().toString();
//
//    if (evt instanceof IdleStateEvent event) {
//      if (event.state() == IdleState.READER_IDLE) {
//        System.out.println("Client: " + socketString + " READER_IDLE 读超时");
//        ctx.disconnect();
//      } else if (event.state() == IdleState.WRITER_IDLE) {
//        System.out.println("Client: " + socketString + " WRITER_IDLE 写超时");
//        ctx.disconnect();
//      } else if (event.state() == IdleState.ALL_IDLE) {
//        System.out.println("Client: " + socketString + " ALL_IDLE 总超时");
//        ctx.disconnect();
//      }
//    }
//  }
//}